package org.example.SlApi.JbxxApi;

import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.apache.commons.codec.net.URLCodec;
import org.example.JycInfo;
import org.example.utils.ConPlsqla;
import org.example.utils.ReadConf;
import org.example.utils.TestDataLoader;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static org.example.RestClient.givenWithCookies;
import static org.example.RestClient.loginAndUpdateCookies;


public class QyjbxxbcApi {//企业基本信息保存
    public static void main(String[] args) throws SQLException, UnsupportedEncodingException, ClassNotFoundException, IOException {


        // 直接在 main 方法中初始化 testDataLoader
        testDataLoader = new TestDataLoader();
        qyjbxxbcApi();
    }
    private static List<JycInfo> JycList = new ArrayList<>();
    private  static TestDataLoader testDataLoader;

    public static String qyjbxxbcApi() throws IOException {
        if (testDataLoader == null) {
            testDataLoader = new TestDataLoader();
        }


        // 登录并更新cookies

        loginAndUpdateCookies();
        RestAssured.baseURI = ReadConf.getHost(); // 设置基础URI
        List<JycInfo> jycInfoList = initJyclist(); // 调用方法来获取列表
        if (!jycInfoList.isEmpty()) {
            JycInfo firstJycInfo = jycInfoList.get(0); // 获取列表中的第一个元素
            String nameid = firstJycInfo.getName_id(); // 使用实际的getter方法
            String sbid = firstJycInfo.getSbid();
            String entid = firstJycInfo.getName_id();
            String entname = firstJycInfo.getEntname();
//            String qylxdl = firstJycInfo.getQylxdl();
            String proloc = firstJycInfo.getProloc();
            Map<String, String> testData = testDataLoader.getTestData();

            String S_DOM_LOCATION = testData.get("S_DOM_LOCATION");

        String S_ENTNAME = entname;
        System.out.println(S_ENTNAME);
        String S_DOM = proloc + S_DOM_LOCATION;
            System.out.println(S_DOM);
//        String S_DOM_LOCATION = "高端护肤的";
        URLCodec urlCodec = new URLCodec();
        String encodedParam1 = URLEncoder.encode(S_ENTNAME, "UTF-8");
        String encodedParam2 = URLEncoder.encode(S_DOM, "UTF-8");
        String encodedParam3 = URLEncoder.encode(S_DOM_LOCATION, "UTF-8");
//        String encodedTextWithPercent1 = encodedParam.replace("%", "%3A");
         String base =  ReadConf.getHost();
            String data = "jbxx=S_ADDRCODE_%3A_%2CS_ADDRCONTENT_%3A_%2CS_DOMPRORIGHT_%3A1_%2CS_ENTNAME_%3A" + encodedParam1 + "_%2CS_REGORG_%3A330202_%2CS_LOCALADM_%3A33020303_%2CS_ENTTYPE_%3A1100_%2CS_QYLXDL_%3A11_%2CS_INDUSTRYPHY_%3A7484_%2CS_NAME_ID_%3A" + nameid + "_%2CS_POSTALCODE_%3A315000_%2CS_DOM_PROVINCE_%3A330000_%2CS_DOM_CITY_%3A330200_%2CS_DOM_DISTRICT_%3A330102_%2CS_DOM_STREET_%3A330102001_%2CS_DOM_LOCATION_%3A" + encodedParam3 + "_%2CS_DOM_%3A" + encodedParam2 + "_%2CS_PROLOC_PROVINCE_%3A330000_%2CS_PROLOC_CITY_%3A330100_%2CS_PROLOC_DISTRICT_%3A330102_%2CS_PROLOC_STREET_%3A330102001_%2CS_PROLOC_LOCATION_%3A" + encodedParam3 + "_%2CS_PROLOC_%3A" + encodedParam2 + "&sbid=" + sbid + "&pripid=&entname=" + encodedParam1 + "&sblx=&entid=" + entid + "&enttype=1200&S_NAME_ID=" + nameid + "_&ljmsign=2&localadm=33010203&S_CERTSEPREGIST=1&S_YZDZ=&S_ISYZDZ=0&zsyzdzArray=&qylxdl=11&sbms=1&S_REGORG=330203&S_QYLXDL=11&addrContent=&addrCode=&label=&S_DOM_PROVINCE=330000&S_DOM_CITY=330200&S_DOM_DISTRICT=330203&S_DOM_STREET=330203001&S_DOM_LOCATION=" + encodedParam3 + "&S_POSTALCODE=310000&S_DOMPRORIGHT=1&szdq=&S_PROVINCE=&S_CITY=&S_DISTRICT=&S_STREET=&S_LOCATION=&zszzsbxx=S_ISCNZSB_%3A0&zszzsbcqr=&S_ISCNZSB=&S_CQZH=&S_ZLBAZH=&S_GFHTH=&S_CQR=&S_CQRLXDH=&S_MJ=&S_SYQQDFS=1&D_SYQXQ=&D_SYQXZ=";
//String data = "method=edit&sbid=2024091126831903&pripid=330102020417019563112&entname=%E9%A1%B6%E9%A1%B6%E9%A1%B6%E9%A1%B6%E9%A1%B6%E5%BB%BA%E7%AD%91%E8%AE%BE%E8%AE%A1%E4%BA%8B%E5%8A%A1%E6%89%80%EF%BC%88%E6%B5%99%E6%B1%9F%EF%BC%89%E6%9C%89%E9%99%90%E8%B4%A3%E4%BB%BB%E5%85%AC%E5%8F%B8&entid=202409111240717204752&enttype=1200&regno=&regorg=330102&qylxdl=11&sbms=1&ljmsign=2&znspsign=0&bdcsign=&name_id=202409111240717204752&S_ITE=0&S_ITENAME=&S_ITEREGNO=&jbxx=S_REGCAP_%3A1000_%2CS_SHDJJG_%3A_%2CS_REGORG_%3A330100_%2CS_LOCALADM_%3A33010203_%2CS_ENTNAME_%3A%E9%A1%B6%E9%A1%B6%E9%A1%B6%E9%A1%B6%E9%A1%B6%E5%BB%BA%E7%AD%91%E8%AE%BE%E8%AE%A1%E4%BA%8B%E5%8A%A1%E6%89%80%EF%BC%88%E6%B5%99%E6%B1%9F%EF%BC%89%E6%9C%89%E9%99%90%E8%B4%A3%E4%BB%BB%E5%85%AC%E5%8F%B8_%2CS_JYQX_%3A999_%2CS_QYLXDL_%3A11_%2CS_INDUSTRYPHY_%3A7484_%2CS_TEL_%3A_%2CS_NAME_ID_%3A202409111240717204752_%2CS_INSFORM_%3A_%2CN_EMPNUM_%3A_%2CS_OPSCOPE_%3A_%2CS_DOMPRORIGHT_%3A1&qyzc=S_JSRS_%3A3_%2CS_JSRS_ZGDB_%3A1_%2CS_DSRS_%3A3_%2CS_FDSZRS_%3A0_%2CS_DSRS_ZGDB_%3A0_%2CS_JLJRQK_%3A0_%2CS_PRIPID_%3A330102020417019563112_%2CS_ZCMB_%3A1106&zyryxgbz=&FZXX=&label=&S_NAME_ID=202409111240717204752&S_ENTNAME=%E9%A1%B6%E9%A1%B6%E9%A1%B6%E9%A1%B6%E9%A1%B6%E5%BB%BA%E7%AD%91%E8%AE%BE%E8%AE%A1%E4%BA%8B%E5%8A%A1%E6%89%80%EF%BC%88%E6%B5%99%E6%B1%9F%EF%BC%89%E6%9C%89%E9%99%90%E8%B4%A3%E4%BB%BB%E5%85%AC%E5%8F%B8&S_DOM_PROVINCE=330000&S_DOM_CITY=330100&S_DOM_DISTRICT=330102&S_DOM_STREET=330102001&S_DOM_LOCATION=%E7%9A%84%E8%BA%AB%E4%BB%BD&S_DOMPRORIGHT=1&S_INDUSTRYPHY=7484&addrContent=&addrCode=&S_SHDJJG=&S_LOCALADM=33010203&S_DOM=%E6%B5%99%E6%B1%9F%E7%9C%81%E6%9D%AD%E5%B7%9E%E5%B8%82%E4%B8%8A%E5%9F%8E%E5%8C%BA%E6%B8%85%E6%B3%A2%E8%A1%97%E9%81%93%E7%9A%84%E8%BA%AB%E4%BB%BD&N_EMPNUM=&S_TEL=&S_REGCAP=1000&S_ENTTYPE=&SFCQ=1&S_ZZFBS=&S_CNO=&S_JYFWPXSIGN=0&S_ISYZDZ=0&S_YZDZ=&S_CERTSEPREGIST=1&S_OPSCOPE=&mainTypeOne=&mainTypeTwo=&mainTypeTre=&chooseopscope=&isrestrict=&jyfw_xkxm=&jyfw_ybxm=&opscope_xkxm=&opscope_ybxm=&opscope_xkxm_isrestrict=&opscope_ybxm_isrestrict=&S_OPSCOPE_HAND=&haveNoPreapprovalitems=&YRGF=0&JS=1&S_JSRS=&S_JSRS_ZGDB=&S_DSRS=&S_FDSZRS=&S_DSRS_ZGDB=&S_JSRS=&S_JSRS_ZGDB=&S_DSRS=3&S_FDSZRS=0&S_DSRS_ZGDB=0&S_JSRS=3&S_JSRS_ZGDB=1&csrftoken=";

            if(JycList.isEmpty()){
                initJyclist();
            }
                    System.out.println(data);
            Response response = givenWithCookies()
                    .body(data)
                    .header("Content-Type", "application/x-www-form-urlencoded")

//                .params(formParams)
                    .log().all()
//               .cookies(cookies)

                    .post("ywtSldj.do?method=saveJbxx&type=0&mk=zsbc&csrftoken="); // 注意：可能需要有效的 CSRF Token
            int Code = Integer.parseInt(String.valueOf(response.getStatusCode()));
            String body1 = response.asString();
            System.out.println("code=" + Code);
            System.out.println(body1);
            if (Code == 302) {

                String redirectUrl = response.getHeader("Location");
                System.out.println(redirectUrl);
                if (redirectUrl != null) {
                    Response finalResponse = givenWithCookies()
                            .when()
                            .get(redirectUrl);
                    int statusCode = finalResponse.getStatusCode();
                    System.out.println("Status Code: " + statusCode);
                    // 获取 HTML 响应体
                    String responseBody = finalResponse.getBody().asString();


                    String body = response.getBody().asString();
                System.out.println(body);
//                if(JycList.isEmpty()){
//                    initJyclist();
//                }

                    return responseBody;
                }
            }

        }
        return null;
    }
    public static  List<JycInfo>  initJyclist() throws IOException {
        if (testDataLoader == null) {
            testDataLoader = new TestDataLoader();
        }
        List<JycInfo> jycInfoList = new ArrayList<>();
        Map<String, String> testData = testDataLoader.getTestData();
        String nameXzqh = testData.get("nameXzqh");
        String nameZh = testData.get("nameZh");
        String nameHy = testData.get("nameHy");
        String nameZzxs = testData.get("nameZzxs");

//        String sql = "SELECT * FROM qcdz_qy_sl_qyhznr WHERE entname LIKE ?" ;
        String sql = "SELECT * FROM QCDZ_MC_SL_QYMCHZK WHERE entname LIKE ?" ;

        String entname = nameZh + nameHy + "（" + nameXzqh + "）" + nameZzxs ;

        try (ResultSet rs = ConPlsqla.exec_select(sql, "%" + entname + "%")){

            while (rs != null && rs.next()) {
                JycInfo jycInfo = new JycInfo();

                jycInfo.setSbid(rs.getString("sbid"));
                jycInfo.setEntname(rs.getString("entname"));

                jycInfo.setName_id(rs.getString("name_id"));// 替换为实际的字段名





                // 填充更多字段
                jycInfoList.add(jycInfo);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        for (JycInfo info : jycInfoList) {
            System.out.println("结果是"+info);
        }
        return jycInfoList;
    }
}

